package org.wyl.job51.component;

import cn.hutool.core.map.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.wyl.job51.config.Job51Config;
import org.wyl.job51.entity.JobInfo;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Json;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Mask.m
 * @create: 2021/07/14 20:15
 * @description:
 */
@Component
public class JobPageProcessor implements PageProcessor {

    @Override
    public Site getSite() {
        Site site = Site.me();
        String[] headerStr = Job51Config.requestHeader.split("\n");
        Arrays.stream(headerStr).forEach(header -> {
            int index = header.indexOf(":");
            String key = header.substring(0, index).trim();
            String value = header.substring(index + 1).trim();
            if ("Cookie" .equals(key)) {
                site.setDomain(Job51Config.domain);
                String[] cookieStr = value.split(";");
                for (String cookieItem : cookieStr) {
                    int index1 = cookieItem.indexOf("=");
                    String cookieKey = cookieItem.substring(0, index1).trim();
                    String cookieValue = cookieItem.substring(index1 + 1).trim();
//                    System.out.println("request.addCookie(\"" + cookieKey + "\", \"" + cookieValue + "\");");
                    site.addCookie(Job51Config.domain, cookieKey, cookieValue);
                }
            } else {
                // 普通请求头
//                System.out.println("request.addHeader(\"" + key + "\", \"" + value + "\");");
                site.addHeader(key, value);
            }
        });
        return site;
    }

    @Override
    public void process(Page page) {
        try {
            // 上下文 可以传递一些数据
            Map<String, Object> extras = page.getRequest().getExtras();
            if(null == extras){
                extras = new HashMap<>();
            }
            String level = null == extras.get("level") ? "list" : extras.get("level").toString();
            switch (level) {
                case "list":
                    this.parseList(page);
                    break;
                case "detail":
                    this.parseDetail(page);
                    break;
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 解析列表页
     */
    private void parseList(Page listPage) {
        Json json = listPage.getJson();
        // 获取所有的目标详情url
        List<String> urlList = json.jsonPath("engine_jds[*].job_href").all();
        for (String url : urlList) {
            Request request = new Request(url);
            request.setExtras(MapUtil.of("level", "detail"));  // 标识位
            listPage.addTargetRequest(request);
        }
    }

    /**
     * 解析详情页
     *
     * @param page
     */
    private void parseDetail(Page page) {
        Html html = page.getHtml();
        String companyName = html.$("p.cname > a:first-child").xpath("///allText()").get();
        String companyAddr = html.$("div.tCompany_main > div:nth-of-type(2) p").xpath("///allText()").get();
        String companyInfo = html.$("div.tCompany_main > div:last-child").xpath("///allText()").get();
        String jobName = html.$("div.cn > h1").xpath("///allText()").get();
        String tempJobStr = html.$("p.msg.ltype").xpath("///allText()").get();
        String jobAddr = "";
        String time = "";
        if (StringUtils.isNotBlank(tempJobStr)) {
            String[] split = tempJobStr.split("|");
            if (split.length > 4) {
                jobAddr = split[0];
                time = split[split.length - 1];
            }
        }
        String jobInfoStr = html.$("div.bmsg.job_msg.inbox p").xpath("///allText()").get();
        String tempSalaryStr = html.$("div.cn strong").xpath("///allText()").get();
        String salaryMin = "";
        String salaryMax = "";
        if (StringUtils.isNotBlank(tempSalaryStr)) {
            String[] salarySplit = tempSalaryStr.split("万")[0].split("-");
            if (salarySplit.length > 1) {
                salaryMin = salarySplit[0];
                salaryMax = salarySplit[1];
            }
        }
        String url = page.getUrl().get();

        // 去保存
        JobInfo jobInfo = new JobInfo();
        jobInfo.setCompanyName(companyName);
        jobInfo.setCompanyAddr(companyAddr);
        jobInfo.setCompanyInfo(companyInfo);
        jobInfo.setJobName(jobName);
        jobInfo.setJobAddr(jobAddr);
        jobInfo.setJobInfo(jobInfoStr);
        jobInfo.setUrl(url);
        jobInfo.setCreateTime(time);
        jobInfo.setSalaryMin(salaryMin);
        jobInfo.setSalaryMax(salaryMax);

        // 交给pipeline来处理
        page.putField("jobInfo", jobInfo);
    }


}